package com.yjz.zpay.utils;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import java.lang.reflect.Type;

/**
 * 
 * @author yjz 
 * created at 2019/1/17 下午9:58
 */

public class JSONParser {

   protected static Gson gson = new Gson();

   public static String toString(Object obj) {
       return gson.toJson(obj);
   }

   /**
    * @param type 类型反射(Class<?>)或反射令牌(TypeToken)
    * @return Object
    * @throws
    * @Description: 将标准JSON字符串反序列化为对象
    */
   public static Object toObject(String jsonString, Object type) {
       if (type instanceof Type) {
           try {
               return gson.fromJson(jsonString, (Type) type);
           } catch (JsonSyntaxException e) {
               e.printStackTrace();
               return null;
           }
       } else if (type instanceof Class<?>) {
           try {
               return gson.fromJson(jsonString, (Class<?>) type);
           } catch (JsonSyntaxException e) {
               e.printStackTrace();
               return null;
           }
       } else {
           throw new RuntimeException("只能是Class<?>或者通过TypeToken获取的Type类型");
       }
   }
}
